﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Text;

namespace PersonaLibrary.Config
{
    public sealed class CustomSettingSection : ConfigurationSection
    {
        [ConfigurationProperty("items")]
        public CustomSettingElement[] Items
        {
            set { this["items"] = value; }
            get { return (CustomSettingElement[])this["items"]; }
        }
    }

    public sealed class CustomSettingElement : ConfigurationElement, INotifyPropertyChanged
    {
        public CustomSettingElement()
        {
            Alphabets = new List<Alphabet> { new RuAlphabet(), new EnAlphabet() };
        }

        public static CustomSettingElement GetDefaultSection
        {
            get
            {
                CustomSettingElement result = new CustomSettingElement();
                result.SetDefault();
                return result;
            }
        }

        [ConfigurationProperty("name", IsKey = true)]
        public string Name
        {
            set { this["name"] = value; }
            get { return (string)this["name"]; }
        }

        [ConfigurationProperty("isAlphabetFilter")]
        public bool IsAlphabetFilter
        {
            get
            {
                return (bool)this["isAlphabetFilter"];
            }
            set
            {
                this["isAlphabetFilter"] = value;
                RisePropertyChanged("IsAlphabetFilter");
            }
        }

        [ConfigurationProperty("isAllowRegister")]
        public bool IsAllowRegister
        {
            get
            {
                return (bool)this["isAllowRegister"];
            }
            set
            {
                this["isAllowRegister"] = value;
                RisePropertyChanged("IsAllowRegister");
            }
        }

        [ConfigurationProperty("alphabetFilterProperty")]
        public string AlphabetFilterProperty
        {
            get
            {
                return (string)this["alphabetFilterProperty"];
            }
            set
            {
                this["alphabetFilterProperty"] = value;
                RisePropertyChanged("AlphabetFilterProperty");
            }
        }

        [ConfigurationProperty("alphabetFilterValue")]
        public string AlphabetFilterValue
        {
            get { return (string)this["alphabetFilterValue"];
            }
            set
            {
                this["alphabetFilterValue"] = value;
                RisePropertyChanged("AlphabetFilterValue");
            }
        }

        [ConfigurationProperty("activeAlphabetIndex")]
        public string ActiveAlphabetIndex
        {
            set 
            { 
                this["activeAlphabetIndex"] = value; 
                RisePropertyChanged("activeAlphabetIndex"); 
            }
            get
            {
                return (string)this["activeAlphabetIndex"];
            }
        }
        
        public Alphabet ActiveAlphabet
        {
            get
            {
                if(Alphabets.Any(x => x.Language == ActiveAlphabetIndex))
                    return Alphabets.SingleOrDefault(x => x.Language == ActiveAlphabetIndex);
                else
                    throw new Exception("Not available ActiveAlphabetIndex value");
            }
        }

        public List<Alphabet> Alphabets { private set; get; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RisePropertyChanged(string name)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        private void SetDefault()
        {
            IsAlphabetFilter = true;
            IsAllowRegister = false;
            AlphabetFilterProperty = "Name";
            AlphabetFilterValue = "a";
            ActiveAlphabetIndex = "рус";
        }
    }
}
